<template>
  <div>
    <el-dialog
      :title="info.isadd ? '管理员添加' : '管理员编辑'"
      :visible.sync="info.isshow"
      @closed="closed"
    >
      <el-form :model="user"
        >{{ user }}
        <el-form-item label="所属角色" label-width="120px" >
          <el-select v-model="user.roleid" placeholder="客服专员">
            <el-option
              v-for="item in rouleList"
              :key="item.id"
              :label="item.rolename"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户名" label-width="120px">
          <el-input v-model="user.username" autocomplete="off"  ></el-input>
        </el-form-item>
        <el-form-item label="密码" label-width="120px">
          <el-input v-model="user.password" autocomplete="off"></el-input>
        </el-form-item>
       
        

       
        <el-form-item label="状态" label-width="120px">
          <el-switch
            v-model="user.status"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="add" v-if="info.isadd"
          >确 定添加</el-button
        >
        <el-button type="primary" @click="updata" v-else>确定修改</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { mapGetters, mapActions } from "vuex";
import { IndexRoutes } from "../../../router";
import { requseradd, reqroleedit,reqroleinfo,requseredit,  requserinfo, reqrolelist} from "../../../http/api";
import { successAlert } from "../../../utils//alert";
export default {
  props: ["info", ],
  data() {
    return {
      // 角色列表出数值
      rouleList:[],
      //   表单数据
      user:{
        roleid:"",
       username:"",
       password:"",
        status: 2,
      },
      // icon集合
     
      // 二级路由
    
    };
  },
  computed: {
    ...mapGetters,
   
  },
  methods: {
    ...mapActions,
    cancel() {
      this.info.isshow = false;
    },
  
    // 清空user
    empty() {
      this.user = {
         roleid:"",
       username:"",
       password:"",
        status: 2,
      };
      // 清空树
     
    },
    // 添加数据
    add() {
      // 添加将树山的数据给user的menus
                       //设置树上的值给user.menus
      requseradd(this.user).then((res) => {
     
        if (res.data.code === 200) {
          successAlert(res.data.msg);
                // 清空·user
        this.empty();
        this.info.isshow = false;
        // 刷新列表
        this.$emit("init");
        }
  
      });
    },
  
    getone(uid) {
      console.log("发请求了", +uid);
       requserinfo({ uid: uid }).then((res) => {
      
        console.log(res.data.code ===200);
        if (res.data.code == 200) {
          this.user = res.data.list;
          // 补个id
          // this.user.id=id;
             this.user.password=""
        }
      });
    },
    // 修改
    updata() {
      // 取出树的数据给menus再发请求
   
      requseredit(this.user).then(res=>{
        if(res.data.code==200){
           successAlert(res.data.msg);
            this.empty();
        this.info.isshow = false;
        // 刷新列表
        this.$emit("init");
        }
        
      })
    },
      closed(){
      // 若果是编辑的关闭 就执行empty
      // 不是添加
      if(!this.info.isadd){
           this.empty()
      }
    },
  },
  mounted() {
    // 请求菜单列表数据
    reqrolelist().then(res=>{
      if(res.data.code==200){
      //  console.log(res);
       this.rouleList=res.data.list
      }
     
    })
  },
};
</script>
 <style lang="less" scoped>
@import "../../../less/index.less";
</style>